package com.ogawa.chair7808.ui.activity;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jeremyliao.liveeventbus.LiveEventBus;
import com.ogawa.base.Constant.CommmandNum;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.base.utils.DownTimer;
import com.ogawa.base.Constant.Constants;
import com.ogawa.base.utils.StringUtils;
import com.ogawa.chair7808.R;
import com.ogawa.chair7808.bean.Device7808StateBean;
import com.ogawa.chair7808.interfaces.OnTimeChoose;
import com.ogawa.chair7808.manager.DataManager;
import com.ogawa.chair7808.manager.TimeBean;
import com.ogawa.chair7808.observer.CloseDetailObserver;
import com.ogawa.chair7808.observer.CloseProfessionalObserver;
import com.ogawa.chair7808.observer.CloseScanAndAdjust;
import com.ogawa.chair7808.observer.TimeObserver;
import com.ogawa.chair7808.ui.iview.IProMasDetailView7506;
import com.ogawa.chair7808.ui.presenter.ProMasDetailPresenter7506;
import com.ogawa.chair7808.utils.Activity7808Utils;
import com.ogawa.chair7808.utils.ModuleUtils;
import com.ogawa.chair7808.view.MoveLayout;
import com.ogawa.chair7808.widget.LayoutBody7506;
import com.ogawa.chair7808.widget.LayoutFunctionView7506;
import com.ogawa.chair7808.widget.LayoutGears5_7506;
import com.ogawa.chair7808.widget.LayoutGears6_7506;

import java.lang.ref.WeakReference;

import static com.ogawa.base.Constant.Constant.LIVE_EVENT_BUS_KEY_END_TIME;
import static com.ogawa.base.Constant.Constant.LIVE_EVENT_BUS_KEY_UPDATE_TIME;
import static com.ogawa.base.utils.StringUtils.numberToTime;


/**
 * 专业按摩 按摩页面
 * Created by FHB on 2018/3/27.
 */

public class ProMasDetailActivity7808 extends BaseMqtt7808Activity implements View.OnClickListener,
        MoveLayout.OnChooseAction, OnTimeChoose
//        ,IProMasDetailView7506 ,TimeObserver.onTimeUpdataLis
//        , CloseDetailObserver.onCloseDetail
{

    private RelativeLayout mLl3D;
    private RelativeLayout mLlStrong;
    private RelativeLayout mLlSpeed;
    private RelativeLayout mLlAir;
    private boolean isFinishShow;
    private ImageView mIvStart;
    private LayoutBody7506 mLayoutBody7506;
    private LayoutFunctionView7506 mLayoutFunction;
    private String mName;
//    private ProMasDetailPresenter7506 mPresenter;


    private LayoutGears6_7506 mLayout3D;
    private LayoutGears5_7506 mLayoutStrong;
    private LayoutGears5_7506 mLayoutSpeed;
    private LayoutGears5_7506 mLayoutAir;
    private String[] name = {"3D", "强度", "速度", "气压强度"};
    private MoveLayout mLayout;
    private ScrollView mScroll;
    private TextView mTvTime;
    private MyHandler myHandler;
    //    private MyCountDownTimer timer;
    private final long INTERVAL = 1000L;
    private long mTime;
    private DownTimer mTimer;


    @Override
    public int setLayoutId() {
        return R.layout.activity_pro_massage_detail_7808;
    }



    @Override
    public void initTitleBar() {
        super.initTitleBar();
        titleBar.setGreenBackBotton();
        titleBar.setTitle(mName);
        setBackGround(R.mipmap.bg);
        setmName("ProMasDetailActivity7506");
    }

    @Override
    public void initView(int layoutId) {
        super.initView(layoutId);
        initView();
        setTimeListener(this);
        LiveEventBus.get(LIVE_EVENT_BUS_KEY_UPDATE_TIME, TimeBean.class).observe(this, new androidx.lifecycle.Observer<TimeBean>() {
            @Override
            public void onChanged(TimeBean timeBean) {
                long min = timeBean.getMin();
                long second = timeBean.getSecond();
                mTvTime.setText("" + numberToTime(min) + " : " + numberToTime(second));
            }
        });
        LiveEventBus.get(LIVE_EVENT_BUS_KEY_END_TIME, Integer.class).observe(this, new androidx.lifecycle.Observer<Integer>() {
            @Override
            public void onChanged(Integer timeBean) {
            }
        });
    }

    private void initView() {
        Log.e("is7506a","ProMasDetailActivity7506");
        mName = getIntent().getStringExtra("name");
        mLl3D = findViewById(R.id.layout_3d_7506);
        mLlStrong = findViewById(R.id.linear_strong_7506);
        mLlSpeed = findViewById(R.id.linear_speed_7506);
        mLlAir = findViewById(R.id.linear_strong_air_7506);
        mTvTime = findViewById(R.id.showTime_7506);
        mLayoutBody7506 = findViewById(R.id.layout_body_view_7506);
        mLayoutFunction = findViewById(R.id.activity_pro_mas_functionview_7506);
        mScroll = findViewById(R.id.activity_detail_scroll_7506);
        mLayout = findViewById(R.id.detail_move_layout_7506);
        mLayout.setData(name);
        mLayout.setScollView(mScroll);
        mLayout.setAction(this);
        mIvStart = findViewById(R.id.imgSwitch_7506);
        mLayout3D = findViewById(R.id.layout_3d_7506);
        mLayoutStrong = findViewById(R.id.linear_strong_7506);
        mLayoutSpeed = findViewById(R.id.linear_speed_7506);
        mLayoutAir = findViewById(R.id.linear_strong_air_7506);
        mLayout3D.setType(1);
        mLayoutStrong.setType(1);
        mLayoutSpeed.setType(2);
        mLayoutAir.setType(3);
        mTvTime.setOnClickListener(this);
        mIvStart.setOnClickListener(this);
        myHandler = new MyHandler(this);

        updateView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLayoutBody7506.onDestory();
        mLayoutFunction.onDestory();
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.imgSwitch_7506) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                DataManager.getInst().sendCommond(CommmandNum.PAUSE,"5");
            }

        } else if (i == R.id.showTime_7506) {
            if (CommonUtil.isFastClick()) {
                return;
            } else {
                showDialog();
            }

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        DataManager.getInst().setHandOutMessageActivity(true);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Activity7808Utils.startProfessionMassaActivity(this);
        this.finish();
    }

    @Override
    public void onChoose(int position) {
        mLayout.setTectColor(position);
        switch (position) {
            case 0:
                mLlStrong.setVisibility(View.GONE);
                mLl3D.setVisibility(View.VISIBLE);
                mLlSpeed.setVisibility(View.GONE);
                mLlAir.setVisibility(View.GONE);


                break;
            case 1:
                mLlStrong.setVisibility(View.VISIBLE);
                mLl3D.setVisibility(View.GONE);
                mLlSpeed.setVisibility(View.GONE);
                mLlAir.setVisibility(View.GONE);
                break;
            case 2:
                mLlStrong.setVisibility(View.GONE);
                mLl3D.setVisibility(View.GONE);
                mLlSpeed.setVisibility(View.VISIBLE);
                mLlAir.setVisibility(View.GONE);
                break;
            case 3:
                mLlStrong.setVisibility(View.GONE);
                mLl3D.setVisibility(View.GONE);
                mLlSpeed.setVisibility(View.GONE);
                mLlAir.setVisibility(View.VISIBLE);
                break;

        }

    }


    @Override
    public void onTimeListener(String time) {
//        mTvTime.setText(time+"");
        if(time.equals(Constants.TIME_5)){
            DataManager.getInst().sendCommond(CommmandNum.TIME_5,"5");
        }
        else if(time.equals(Constants.TIME_10)){
            DataManager.getInst().sendCommond(CommmandNum.TIME_10,"10");
        }
        else if(time.equals(Constants.TIME_15)){
            DataManager.getInst().sendCommond(CommmandNum.TIME_15,"15");
        }
        else if(time.equals(Constants.TIME_20)){
            DataManager.getInst().sendCommond(CommmandNum.TIME_20,"20");
        }
        else if(time.equals(Constants.TIME_25)){
            DataManager.getInst().sendCommond(CommmandNum.TIME_25,"25");
        }
        else if(time.equals(Constants.TIME_30)){
            DataManager.getInst().sendCommond(CommmandNum.TIME_30,"30");
        }

//        startTimer(Integer.valueOf(time.substring(0,time.indexOf(":")).trim()),Integer.valueOf(time.substring(time.length()-2,time.length() )));
    }


    void updateView(){
        Device7808StateBean device7808StateBean = DataManager.getInst().getDevice7808StateBean();
        mIvStart.setSelected("1".equals(device7808StateBean.getRunningStatus()));
        mLayout3D.setShow(StringUtils.getValue(device7808StateBean.get_3DPosIconBean()));
        mLayoutStrong.setShow(StringUtils.getValue(device7808StateBean.getMassageStrength()));
        mLayoutSpeed.setShow(StringUtils.getValue(device7808StateBean.getMassageSpeed()));
        mLayoutAir.setShow(StringUtils.getValue(device7808StateBean.getAirIntensity()));
        titleBar.setTitle(ModuleUtils.getName());

//        if(DataManager.getInst().getValue(CommmandNum.mStatusShoulderScanning).equals("1")){
//            Log.e("detaill1",DataManager.getInst().getValue(CommmandNum.mStatusShoulderScanning)+"");
//            Activity7808Utils.startScanShoulderActivity(this);
//        }else if(DataManager.getInst().getValue(CommmandNum.mStatusShoulderAdjust).equals("1")){
//            Activity7808Utils.startShouldersAdjustmentActivity(this);
//
//        }
    }

//
//    @Override
//    public void onupdataTime(String min, String sec) {
//        if(!(min.equals("0")&&sec.equals("0"))){
//            long time = (Integer.valueOf(min)*60+Integer.valueOf(sec));
//            Log.e("updataTime_detail",time+"==="+mTime);
//            if(Math.abs(time-mTime)>30){
//                setTime(Integer.valueOf(min),Integer.valueOf(sec));
//            }
//        }else {
//
//            Log.e("updataTime_detail222",min+"==="+mTime);
//            mTvTime.setText("00 : 00");
//        }
//
//    }
//
//    @Override
//    public void onClose() {
//        Activity7808Utils.startProfessionMassaActivity(this);
//        this.finish();
//
//    }

    @Override
    public void changeViewByMqttMessage() {
        updateView();
    }


    private class MyHandler extends Handler {
        WeakReference<ProMasDetailActivity7808> mProMasDetailActivityWeakReference;

        MyHandler(ProMasDetailActivity7808 mProMasDetailActivity) {
            mProMasDetailActivityWeakReference = new WeakReference<>(mProMasDetailActivity);
        }

        @Override
        public void handleMessage(Message msg) {
            final ProMasDetailActivity7808 mHomeActivity = mProMasDetailActivityWeakReference.get();
            if (mHomeActivity != null) {
                switch (msg.what) {
                    case 1:
                        mLayout3D.setShow((int) msg.obj);
                        break;


                }
            }
        }
    }


//
//    @Override
//    public void getName(String name) {
//        Log.e("toProMasDetailActivity2",name+"");
//        mName = name;
//        titleBar.setTitle(name);
//    }


}
